package edu.ayit.controller;

import edu.ayit.pojo.Blog;
import edu.ayit.pojo.Tag;
import edu.ayit.service.BlogService;
import edu.ayit.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Controller
public class TagShowController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private TagService tagService;

    @GetMapping("/tags/{id}")
    public String tagList(@PageableDefault(size = 8,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
                           @PathVariable Long id,
                           Model model){

        List<Tag> tags = tagService.getListTop(10000);
        if(id == -1){
            id = tags.get(0).getId();
        }
        System.out.println("------------------------------------------");
        System.out.println(tags.get(0).getBlogs().size());
        Page<Blog> blogs = blogService.listBlog(pageable, id);
        model.addAttribute("page",blogs);
        model.addAttribute("tags",tags);
        model.addAttribute("activeTagId",id);
        return "tags";

    }
}
